package in.muton.edushare.processing.objects;

import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserBatchLink;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UserDataHandler implements Serializable {

	private static final long serialVersionUID = 9109103543977755043L;

	public UserDataHandler(IModel<Users> user) {
		this.user = user;
	}

	public IModel<Universities> getMainUniv() {
		List<UserRoles> uR = user.getObject().getRoleList();
		for (UserRoles r : uR) {
			if (r.getStatus() == UserRoles.STATUS_ACTIVE)
				return new CayenneModel<Universities>(r.getUniversity());
		}
		return null;
	}

	public CayenneList<Universities> getUniv() {
		List<UserRoles> uR = user.getObject().getRoleList();
		CayenneList<Universities> cL = new CayenneList<Universities>(null);
		for (UserRoles r : uR) {
			if (r.getStatus() == UserRoles.STATUS_ACTIVE)
				cL.add(r.getUniversity());
		}
		return cL;
	}

	public IModel<Departments> getMainDept() {
		List<UserDeptLink> uD = user.getObject().getDepartmentLinks();
		for (UserDeptLink u : uD) {
			if (u.getStatus() == UserDeptLink.STATUS_ACTIVE)
				return new CayenneModel<Departments>(u.getDepartmentLink());
		}
		return null;
	}

	public CayenneList<Departments> getAllDepts() {
		List<UserDeptLink> uD = user.getObject().getDepartmentLinks();
		CayenneList<Departments> cL = new CayenneList<Departments>(null);
		for (UserDeptLink u : uD) {
			if (u.getStatus() == UserDeptLink.STATUS_ACTIVE)
				cL.add(u.getDepartmentLink());
		}
		return cL;
	}

	public CayenneList<CourseInstances> getActiveCourseInst() {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<CourseInstances> cL = new ArrayList<CourseInstances>();
		for (Enrolments e : eL) {
			if (e.getCourseInstance().getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
					|| e.getCourseInstance().getStatus() == CourseInstances.COURSE_OPEN_STANDALONE) {
				if (e.getStatus() != Enrolments.INVITED)
					cL.add(e.getCourseInstance());
			}
		}
		Collections.sort(cL);
		return new CayenneList<CourseInstances>(cL);
	}

	public CayenneList<CourseInstances> getActiveCourses() {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<CourseInstances> cL = new ArrayList<CourseInstances>();
		for (Enrolments e : eL) {
			if (e.getCourseInstance().getStatus() == CourseInstances.COURSE_OPEN_LINKED_TO_BATCH
					|| e.getCourseInstance().getStatus() == CourseInstances.COURSE_OPEN_STANDALONE) {
				if (e.getStatus() != Enrolments.INVITED)
					cL.add(e.getCourseInstance());
			}
		}
		Collections.sort(cL);
		return new CayenneList<CourseInstances>(cL);
	}

	public CayenneList<CourseInstances> getAllCourseInst() {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<CourseInstances> cL = new ArrayList<CourseInstances>();
		for (Enrolments e : eL) {
			if (e.getStatus() != Enrolments.INVITED)
				cL.add(e.getCourseInstance());
		}
		Collections.sort(cL);
		return new CayenneList<CourseInstances>(cL);
	}

	public CayenneList<Courses> getAllCourses() {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<Courses> cL = new ArrayList<Courses>();
		for (Enrolments e : eL) {
			if (e.getStatus() != Enrolments.INVITED)
				cL.add(e.getCourseInstance().getCourse());
		}
		Collections.sort(cL);
		return new CayenneList<Courses>(cL);
	}

	public CayenneList<CourseInstances> getCompletedCourses() {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<CourseInstances> cL = new ArrayList<CourseInstances>();
		for (Enrolments e : eL) {
			if (e.getStatus() != Enrolments.INVITED
					&& e.getCourseInstance().getStatus() == CourseInstances.COURSE_CLOSED)
				cL.add(e.getCourseInstance());
		}
		Collections.sort(cL);
		return new CayenneList<CourseInstances>(cL);
	}

	public CayenneList<Curricula> getCurricula() {
		List<UserBatchLink> uB = user.getObject().getUserBatchLink();
		CayenneList<Curricula> cL = new CayenneList<Curricula>(null);
		for (UserBatchLink u : uB) {
			cL.add(u.getBatch().getCurricula());
		}
		return cL;
	}

	public CayenneList<CourseInstances> getCourseInstByRole(int role) {
		List<Enrolments> eL = user.getObject().getEnrolments();
		List<CourseInstances> cL = new ArrayList<CourseInstances>();
		for (Enrolments e : eL) {
			if (e.getEnrolmentType() == role
					&& e.getStatus() != Enrolments.INVITED)
				cL.add(e.getCourseInstance());
		}
		Collections.sort(cL);
		return new CayenneList<CourseInstances>(cL);
	}

	public boolean isStudent() {
		List<UserRoles> uR = user.getObject().getRoleList();
		for (UserRoles r : uR) {
			if (r.getRoleType() == UserRoles.STUDENT_ROLE
					&& r.getStatus() == UserRoles.STATUS_ACTIVE)
				return true;
		}

		List<UserDeptLink> udL = user.getObject().getDepartmentLinks();
		for (UserDeptLink u : udL) {
			if (u.getRoleType() == UserDeptLink.STUDENT
					&& u.getStatus() == UserDeptLink.STATUS_ACTIVE)
				return true;
		}

		List<Enrolments> enrol = user.getObject().getEnrolments();
		for (Enrolments e : enrol) {
			if ((e.getEnrolmentType() == Enrolments.STUDENT || e
					.getEnrolmentType() == Enrolments.OBSERVER)) {
				if (e.getStatus() != Enrolments.INVITED)
					return true;
			}
		}
		return false;
	}

	private IModel<Users> user;
}
